import axios from 'axios';
import type { WetherList } from '@/types/wether';
import { onMounted, ref } from 'vue';
/**
 * 获取当地天气钩子
 * @returns { object }  tem-温度,wea-天气
 */
export default function () {
  let wea = ref<string>('');
  let tem = ref<string>('');
  // 获取天气
  onMounted(() => {
    getWeaAndTem();
  })
  const getWeaAndTem = () => {

    axios({
      method: 'GET',
      url: "https://api.seniverse.com/v3/weather/now.json",
      params: {
        key: 'S0iWKy23OH6dJRMdo',
        location: 'zhengzhou',
        language: 'zh-Hans',
        unit: 'c'
      }
    }).then((res) => {
      const weatherList: WetherList = res.data.results;
      tem.value = weatherList[0].now.temperature;
      wea.value = weatherList[0].now.text;
    }).catch((error) => {
      console.log(error);
    });
  }
  return { tem, wea }
}